using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Fated.Graphics {
    public class Border {
        private Texture2D borderTexture;

        public Border(Texture2D input) {
            borderTexture = input;
            Rectangle bounds = borderTexture.Bounds;
            Position = Vector2.Zero;
            Size = new Vector2(bounds.Width, bounds.Height);
            Depth = 0.01f;
        }
        public Vector2 Size { get; set; }
        // Upper left corner
        public Vector2 Position { get; set; }
        public float Depth { get; set; }
        public int SideBorderThickness {
            get {
                return borderTexture.Bounds.Width / 2;
            }
        }
        public int TopBorderThickness {
            get {
                return borderTexture.Bounds.Height / 2;
            }
        }

        public void drawBorder(SpriteBatch sB) {
            Rectangle bounds = borderTexture.Bounds;
            int width = bounds.Width / 2 - 1;
            int height = bounds.Height / 2 - 1;
            drawCorners(sB, bounds, width, height);
            drawCenter(sB, bounds, width, height);
            drawEdges(sB, bounds, width, height);
        }

        private void drawCenter(SpriteBatch sB, Rectangle bounds, int width, int height) {
            Rectangle source = new Rectangle(width, height, 1, 1);
            Rectangle dest = new Rectangle((int)Position.X + width, (int)Position.Y + height,
                (int)Size.X - 2 * width, (int)Size.Y - 2 * height);

            sB.Draw(borderTexture, dest, source, Color.White, 0, Vector2.Zero, SpriteEffects.None, Depth);
        }

        private void drawCorners(SpriteBatch sB, Rectangle bounds, int width, int height) {
            Rectangle topSource = new Rectangle(0, 0, width, height);
            Rectangle topDest = new Rectangle((int)Position.X, (int)Position.Y,
                width, height);

            sB.Draw(borderTexture, topDest, topSource, Color.White, 0, Vector2.Zero, SpriteEffects.None, Depth);
            topSource.X = bounds.Width - width;
            topDest.X = (int)(Position.X + Size.X - width);
            sB.Draw(borderTexture, topDest, topSource, Color.White, 0, Vector2.Zero, SpriteEffects.None, Depth);


            Rectangle bottSource = new Rectangle(0, bounds.Height - height, width, height);
            Rectangle bottDest = new Rectangle((int)Position.X, (int)(Position.Y + Size.Y - height),
                width, height);

            sB.Draw(borderTexture, bottDest, bottSource, Color.White, 0, Vector2.Zero, SpriteEffects.None, Depth);
            bottSource.X = bounds.Width - width;
            bottDest.X = (int)(Position.X + Size.X - width);
            sB.Draw(borderTexture, bottDest, bottSource, Color.White, 0, Vector2.Zero, SpriteEffects.None, Depth);
        }
        private void drawEdges(SpriteBatch sB, Rectangle bounds, int width, int height) {

            Rectangle topSource = new Rectangle(width, 0, 1, height);
            Rectangle topDest = new Rectangle((int)Position.X + width, (int)Position.Y,
                (int)(Size.X - 2 * width), height);

            sB.Draw(borderTexture, topDest, topSource, Color.White, 0, Vector2.Zero, SpriteEffects.None, Depth);

            topSource.Y = (int)(bounds.Height - height);
            topDest.Y = (int)(Position.Y + Size.Y - height);
            sB.Draw(borderTexture, topDest, topSource, Color.White, 0, Vector2.Zero, SpriteEffects.None, Depth);

            Rectangle leftSource = new Rectangle(0, height, width, 1);
            Rectangle leftDest = new Rectangle((int)Position.X, (int)Position.Y + height,
                width, (int)(Size.Y - 2 * height));

            sB.Draw(borderTexture, leftDest, leftSource, Color.White, 0, Vector2.Zero, SpriteEffects.None, Depth);

            leftSource.X = (int)(bounds.Width - width);
            leftDest.X = (int)(Position.X + Size.X - width);
            sB.Draw(borderTexture, leftDest, leftSource, Color.White, 0, Vector2.Zero, SpriteEffects.None, Depth);
        }
    }
}
